package com.nutransit.commoncode;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import com.nutransit.R;

public class MySpinnerAdapter extends BaseAdapter implements SpinnerAdapter, Webservice_parametrs
{
	private JSONArray jsonArray;
	Context context;
	public MySpinnerAdapter(Context context1,JSONArray jsonArray) 
	{
		this.context = context1;
		this.jsonArray = jsonArray;
	}

	public int getCount()
	{
		return jsonArray.length();
	}

	public Object getItem(int position) 
	{
		try
		{
			return jsonArray.get(position);
		}
		catch (JSONException e)
		{
			e.printStackTrace();
			return "";
		}
	}

	public long getItemId(int position)
	{
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) 
	{
		try
		{
			if (convertView == null)
			{
				LayoutInflater inflater = (LayoutInflater) context.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(R.layout.row_spinner,parent, false);
			}

			TextView tv_Spinner_Item = (TextView) convertView .findViewById(R.id.tv_Spinner_Item);
			TextView tv_id = (TextView) convertView .findViewById(R.id.tv_id);

			JSONObject jsonObject = jsonArray.getJSONObject(position);
			if(jsonObject.has(TAG_country_name))
			{
				tv_Spinner_Item.setText(jsonObject.getString(TAG_country_name));
				tv_id.setText(jsonObject.getString(TAG_country_id));
			}
			else if(jsonObject.has(TAG_state_name))
			{
				tv_Spinner_Item.setText(jsonObject.getString(TAG_state_name));
				tv_id.setText(jsonObject.getString(TAG_state_id));
			}
			else if(jsonObject.has(TAG_name))
			{
				tv_Spinner_Item.setText(jsonObject.getString(TAG_name));
				tv_id.setText(jsonObject.getString(TAG_id));
			}
			else if(jsonObject.has(TAG_cname))
			{
				tv_Spinner_Item.setText(jsonObject.getString(TAG_cname));
				tv_id.setText(jsonObject.getString(TAG_cid));
			}
			else if(jsonObject.has(TAG_vname))
			{
				tv_Spinner_Item.setText(jsonObject.getString(TAG_vname));
				tv_id.setText(jsonObject.getString(TAG_vid));
			}
			else if(jsonObject.has(TAG_address))
			{
				tv_Spinner_Item.setText(jsonObject.getString(TAG_city));
				tv_id.setText(jsonObject.getString(TAG_id));
			}
			else if(jsonObject.has(TAG_CCV))
			{
				tv_Spinner_Item.setText(jsonObject.getString(TAG_van));
				tv_id.setText(jsonObject.getString(TAG_id));
			}
		}
		catch(JSONException e)
		{
			e.printStackTrace();
		}
		return convertView;
	}

}
